<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" width="500px" append-to-body custom-class="import-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="房间" prop="houseId">
          <el-row>
            <el-col :span="18">
              <el-input v-model="fullHouseName" placeholder="" style="width: 240px" disabled />
            </el-col>
            <el-col :span="6">
              <!-- <el-button @click="selectHouse">选择</el-button> -->
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="预计开始时间" prop="scheduledTime">
          <el-date-picker v-model="form.scheduledTime" style="width: 240px" format="yyyy-MM-dd HH:mm"
            value-format="yyyy-MM-dd HH:mm" type="datetime" placeholder="选择日期"></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">保 存</el-button>
      </div>
    </el-dialog>
    <selectHouse v-model="selectHouseVisible" @success="selectHouseCb" />
  </div>
</template>

<script>
import { debounce } from "@/utils";
import selectHouse from './selectHouse.vue';
import { addSweepInspection } from '@/api/houseCleaning/sweep'
export default {
  components: {
    selectHouse
  },
  props: {
    title: {
      default: "同一人验房打扫",
    },
    value: {
      default: false,
    },
    data: {
      default: undefined,
    },
    orderInfo:{
      default:{}
    }
  },
  data() {
    return {
      visible: false,
      //是否显示选择房间弹窗
      selectHouseVisible: false,
      fullHouseName: '',
      form: {},
      rules: {
        // houseId: [
        //   { required: true, message: '请选择房间', trigger: 'blur' }
        // ],
        scheduledTime: [
          { required: true, message: '请选择预计开始时间', trigger: 'blur' }
        ]
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 总条数
      total: 0,
      //列表
      list: [],
    };
  },
  watch: {
    value(newVal, oldVal) {
      if (newVal) {
        this.init();
      }
      this.visible = newVal; // 监测value的变化，并赋值。
    },
    visible(val) {
      this.$emit("input", val); // 此处监测visible目的为关闭弹窗时，重新更换value值，注意emit的事件一定要为input。
    },
  },
  methods: {
    init() {
      this.reset()
    },
    reset() {
      // this.fullHouseName = this.orderInfo.house.estateName + this.orderInfo.house.buildingName + this.orderInfo.house.unitName + '单元' + this.orderInfo.house.roomNumber + '号'
      this.fullHouseName = this.orderInfo.houseNew ? this.orderInfo.houseNew.premisesAddress : this.orderInfo.house.premisesAddress
      this.form = {
        // houseId: '',
        orderId:this.orderInfo.orderId,
        scheduledTime: this.$moment().format('YYYY-MM-DD HH:mm')
      }
      this.resetForm("form");
    },
    //选择房间
    selectHouse() {
      this.selectHouseVisible = true
    },
    //选择房间回调
    selectHouseCb(data) {
      if (!data.houseId) return
      this.form.houseId = data.houseId;
      this.fullHouseName = data.estateName + data.buildingName + data.unitName + '单元' + data.roomNumber + '号'
    },
    submitForm: debounce(function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          addSweepInspection(this.form).then(response => {
            if (response.code == 200) {
              this.$emit("success");
              this.$message.success(response.msg);
              this.visible = false;
            } else {
              this.$message.error(response.msg);
            }
          })
        }
      })
    }, 500),
    cancel() {
      this.reset();
      this.visible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/dialog.scss";
</style>
